package com.example.douyin.cons;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.douyin.R;
import com.example.douyin.adapter.ConsAdapter;
import com.example.douyin.databinding.FragmentConsBinding;
import com.example.lib_base_mvvm.baseMVVM.BaseView.BaseFragmnet;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;

import java.util.ArrayList;
import java.util.List;


public class ConsFragment extends BaseFragmnet<FragmentConsBinding,ConsViewModel> {

    List<EMConversation> conversations = new ArrayList<>();
    ConsAdapter consAdapter;

    public ConsFragment() {
        // Required empty public constructor
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_cons;
    }

    @Override
    public void initView() {
        consAdapter = new ConsAdapter(R.layout.item_cons,conversations);
        dataBinding.rv.setAdapter(consAdapter);
        dataBinding.rv.setLayoutManager(new LinearLayoutManager(getContext()));

        viewModel.cons();
        viewModel.success.observe(this, new Observer<List<EMConversation>>() {
            @Override
            public void onChanged(List<EMConversation> emConversations) {
                conversations.clear();
                conversations.addAll(emConversations);
                consAdapter.notifyDataSetChanged();
            }
        });
        //点击跳转到详情页
        consAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                String conversationId = conversations.get(position).conversationId();
                EMMessage.ChatType type = conversations.get(position).getLastMessage().getChatType();
                int chatType = 0;
                if(type== EMMessage.ChatType.Chat){//单聊
                    chatType=0;
                } else if (type== EMMessage.ChatType.GroupChat) {//群聊
                    chatType = 1;
                }
                Bundle bundle = new Bundle();
                bundle.putString("conversationId",conversationId);
                bundle.putInt("chatType",chatType);
                ARouter.getInstance().build("/app/ChatIMActivity").withBundle("bundle",bundle).navigation();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public Class<ConsViewModel> getViewModel() {
        return ConsViewModel.class;
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.cons();
    }
}